# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import string

# 个数
N = 10


def pd_Index():
    # 索引Index
    index: pd.Index = pd.Index(data=list(string.ascii_uppercase[:N]))
    print(type(index))  # pandas.core.indexes.base.Index

    index = index.append(other=pd.Index(data=['X', ]))
    index = index.insert(loc=5, item='Y')

    index = index.delete(5)
    index = index.drop('X')

    pass


def pd_Series():
    # 一维:Series
    series: pd.Series = pd.Series(data=np.arange(N), index=list(string.ascii_uppercase[:N]))
    print(type(series))  # pandas.core.series.Series
    # 属性
    print(type(series.values))  # numpy.ndarray

    index: pd.Index = series.index

    # 切片
    # series[1]
    # series[2:8:4]
    #
    # series[series > 5]
    #
    # series[[0, 1]]
    # series[['A', 'B']]

    # Series转DataFrame
    df = series.to_frame()

    series['K'] = 10

    # 两个Series合并，追加模式
    series1: pd.Series = pd.Series(data=np.arange(start=N, stop=2 * N),
                                   index=list(string.ascii_uppercase[-N:]))
    # series2: pd.Series = series.append(to_append=series1)

    pass


def py_DataFrame():
    # 二维:DataFrame
    df: pd.DataFrame = pd.DataFrame(data=np.arange(0, N * N, 1).reshape(N, N),
                                    index=list(string.ascii_lowercase[:N]),
                                    columns=list(string.ascii_uppercase[:N]))
    print(type(df))  # pandas.core.frame.DataFrame

    # 属性
    print(df.index)  # 索引
    print(df.columns)  # 列
    print(df.dtypes)  # 元素数据类型
    print(df.values)  # 数据

    # 方法
    print(df.info())  # 信息
    print(df.describe())  # 描述

    # 默认显示五行, 可指定显示多少行
    print(df.head())  # 首部
    print(df.tail())  # 尾部

    for index, row in df.iterrows():
        print(row['A'])
        print(row.A)
        pass

    for row in df.itertuples():
        print(row.A)
        pass

    for col_name, col in df.iteritems():
        print(col['a'])
        print(col.a)
        pass

    pass


def pd_pivot_table():
    """
    数据透视表excel&pandas



    """
    df: pd.DataFrame = pd.DataFrame(data=[['000001', '2022-01-31', 12.3],
                                          ['000001', '2022-02-28', 45.6],
                                          ['000002', '2022-01-31', 78.9]],
                                    index=list(string.ascii_lowercase[:3]),
                                    columns=list(string.ascii_uppercase[:3]))
    # 行转列分组对齐
    pt: pd.DataFrame = pd.pivot_table(df, index=['B'], columns=['A'], values=['C'])

    pass


if __name__ == '__main__':
    # pd_Index()
    # pd_Series()
    py_DataFrame()
    # pd_pivot_table()

    pass
